//
//  QuestionCardView.m
//  深呼吸
//
//  Created by rimi on 15/12/24.
//  Copyright © 2015年 RIMI. All rights reserved.
//

#import "QuestionCardView.h"
#import "TextViewController.h"

#define WIDTH_SCREEN ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT_SCREEN ([UIScreen mainScreen].bounds.size.height)
#define WIDTH (WIDTH_SCREEN*0.65)
#define HEIGHT (HEIGHT_SCREEN*0.65)
#define HEIGHT_CELL (0.12 * HEIGHT)

@interface QuestionCardView() <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *orangeDot;

@end

@implementation QuestionCardView

#pragma mark - init
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.bounds = CGRectMake(0, 0, WIDTH, HEIGHT);
        
        
    }
    return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [self initializeAppreace];
}
- (void)initializeAppreace
{
    self.layer.cornerRadius = WIDTH / 15;
    self.clipsToBounds = true;
    [self addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];


    if (indexPath.row < self.dataArray.count) {
        
        
        cell.textLabel.text = self.dataArray[indexPath.row];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        CAShapeLayer *shape = [CAShapeLayer layer];
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake( WIDTH *0.15,HEIGHT_CELL - 2)];
        [path addLineToPoint:CGPointMake( WIDTH *0.85,HEIGHT_CELL - 2)];
        shape.path = path.CGPath;
        shape.strokeColor = [UIColor grayColor].CGColor;
        [cell.layer addSublayer:shape];
        
    }
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *view = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*0.4)];
    view.text = self.title;
    view.textAlignment = NSTextAlignmentCenter;
    view.tintColor = self.titleBackgroundColor;
    view.backgroundColor = self.titleBackgroundColor;
    view.font = [UIFont systemFontOfSize:20];
    view.numberOfLines = -1;
    view.textColor = [UIColor whiteColor];
    
    return view;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor = self.titleBackgroundColor;
    
}
-(void)setTitleBackgroundColor:(UIColor *)titleBackgroundColor
{
    _titleBackgroundColor = titleBackgroundColor;
    [self.tableView reloadSectionIndexTitles];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.didSelectedCellBlock) {
        self.didSelectedCellBlock(indexPath.row);
    }
   
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell addSubview:self.orangeDot];
    [cell bringSubviewToFront:self.orangeDot];
    cell.textLabel.textColor = [UIColor orangeColor];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.textLabel.textColor = [UIColor blackColor];
}
#pragma mark - getter
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.showsVerticalScrollIndicator = false;
        _tableView.rowHeight = HEIGHT_CELL;
        _tableView.scrollEnabled = false;
        _tableView.sectionHeaderHeight = 0.4 * HEIGHT;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (UIView *)orangeDot
{
    if (!_orangeDot) {
        _orangeDot = [[UIView alloc]init];
        _orangeDot.bounds = CGRectMake(0, 0, 8, 8);
        _orangeDot.backgroundColor = [UIColor orangeColor];
        _orangeDot.tintColor = [UIColor orangeColor];
        _orangeDot.clipsToBounds = true;
        _orangeDot.layer.cornerRadius = 4;
        _orangeDot.center = CGPointMake(HEIGHT_CELL/2, HEIGHT_CELL/2);
    }
    return _orangeDot;
}


@end
